package com.qf.service.impl;

import com.google.common.base.Supplier;
import com.google.common.cache.LoadingCache;
import com.qf.cache.SceneCache;
import com.qf.entity.SysScene;
import com.qf.entity.SysSceneExample;
import com.qf.entity.SysUserInfo;
import com.qf.exceptions.AddDataException;
import com.qf.exceptions.DeleteDataException;
import com.qf.exceptions.ResultCode;
import com.qf.exceptions.UpdateException;
import com.qf.mapper.SysSceneDao;
import com.qf.mapper.SysSceneMapper;
import com.qf.pojo.BaseUser;
import com.qf.pojo.CheckType;
import com.qf.service.SceneService;
import com.qf.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @Author ztf
 * @Date 21:53 2022/1/2
 **/
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class SceneServiceImpl implements SceneService {

    private SysSceneDao sysSceneDao;
    private SysSceneMapper sysSceneMapper;
    private SceneCache sceneCache;

    @Autowired
    public void setSceneCache(SceneCache sceneCache) {
        this.sceneCache = sceneCache;
    }

    @Autowired
    public void setSysSceneDao(SysSceneDao sysSceneDao) {
        this.sysSceneDao = sysSceneDao;
    }

    @Autowired
    public void setSysSceneMapper(SysSceneMapper sysSceneMapper) {
        this.sysSceneMapper = sysSceneMapper;
    }

    @Override
    public void addScene(SysScene scene) {
        Assert.isTrue(!scene.isEmpty(CheckType.ADD), () -> {
            throw new AddDataException("SceneServiceImpl：addScene，必传参数不完整", ResultCode.USERNAME_NULL);
        });
        //不允许一个用户创建相同名字的场景
        BaseUser userInfo = SecurityUtils.getUser();
        //查询数据
        SysSceneExample sysSceneExample = new SysSceneExample();
        SysSceneExample.Criteria sysSceneExampleCriteria = sysSceneExample.createCriteria();
        sysSceneExampleCriteria.andSceneNameEqualTo(scene.getSceneName());
        sysSceneExampleCriteria.andCreateByEqualTo(userInfo.getUserId());
        List<SysScene> currenScene = sysSceneDao.selectByExample(sysSceneExample);
        Assert.isTrue(currenScene.isEmpty() || currenScene.get(0).getStatus() != 1, () -> {
            throw new AddDataException("SceneServiceImpl：addScene，场景已存在", ResultCode.DATA_ALREADY_EXIST);
        });
        //如果场景存在则更新数据，不存在则插入数据
        if (currenScene.isEmpty()) {
            scene.setCreateBy(userInfo.getUserId());
            scene.setCreateTime(new Date());
            sysSceneDao.insert(scene);
        } else {
            currenScene.get(0).setCreateTime(new Date());
            sysSceneDao.updateByPrimaryKey(currenScene.get(0));
        }
        //清除缓存
        sceneCache.getSceneLoadingCache().invalidateAll();
    }

    @Override
    public void updateScene(SysScene scene) {
        Assert.isTrue(!scene.isEmpty(CheckType.UPDATE), () -> {
            throw new UpdateException("SceneServiceImpl：updateScene，必传参数不完整", ResultCode.USERNAME_NULL);
        });
        sysSceneDao.updateByPrimaryKey(scene);
        sceneCache.getSceneLoadingCache().invalidateAll();
    }

    @Override
    public void deleteSceneById(List<Long> sceneIds) {
        BaseUser userInfo = SecurityUtils.getUser();
        int result = sysSceneMapper.deleteSceneById(sceneIds,userInfo.getUserId());
        Assert.isTrue(result>0, ()->{
            throw new DeleteDataException("deleteSceneById：删除场景失败",ResultCode.DATA_DELETION_FAILED);
        });
        sceneCache.getSceneLoadingCache().invalidateAll();
    }

    @Override
    public List<SysScene> findByUserId(Integer id) {
        try {
            List<SysScene> scenes = sceneCache.getSceneLoadingCache().get(id);
            return scenes;
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return null;
    }


}
